import {getRequest} from "../api/api"

export const initMenu = (router,store)=>{
    if(store.state.router.length>0){
        return;
    }

    getRequest("/system").then(data=>{
        if(data){
            //格式化router
            let fmtRoutes = formatRoutes(data);
            //添加如路由
            router.addRoutes(fmtRoutes)
            //将数据导入vuex
            store.commit('initRoutes',fmtRoutes)
        }
    })
}

export const formatRoutes = (routes) =>{
    let fmtRoutes = [];
    routes.forEach(router=>{
        let{
            path,
            component,
            name,
            iconcls,
            children,
        }= router
        if(children && children instanceof Array){
            //递归
            children = formatRoutes(children);
        }
        let fmtRouter = {
            path,
            name,
            iconcls,
            children,
            component(resolve){
                require([''].resolve)
            }
        }
        fmtRoutes.push(fmtRouter)
    });
    return fmtRoutes
}
